package com.youlu.campus.admin.course.convert;

import com.youlu.campus.admin.course.dto.ActivityCourseSaveDTO;
import com.youlu.campus.admin.course.dto.CourseVideoSaveDTO;
import com.youlu.campus.admin.course.vo.UserCourseOrderDetailVo;
import com.youlu.campus.entity.WechatMiniUserPO;
import com.youlu.campus.entity.course.ActivityCourse;
import com.youlu.campus.entity.course.ActivityCourseVideo;
import com.youlu.campus.entity.course.UserCourseOrder;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author : [dengbo]
 * @className : ActivetyCourse
 * @description : [描述说明该类的功能]
 * @createTime : [2022/5/16 18:37]
 */
@Service
public class CourseConvert {

    @Autowired
    private MapperFacade mapperFacade;

    public ActivityCourse activityCourseSaveDTOToEntity(ActivityCourseSaveDTO dto, ActivityCourse course) {
        if (course == null) {
            course = new ActivityCourse();
            course.setCreatedTime(new Date());
            course.setDeleted(false);
        }
        course.setName(dto.getName());
        course.setAlias(dto.getName());
        course.setDescription(dto.getDescription());
        course.setPrice(dto.getPrice());
        course.setCreatorId(dto.getCreatorId());
        course.setOriginalPrice(dto.getOriginalPrice());
        if(dto.getCourseType() == null){
            course.setCourseType(0);
        }else{
            course.setCourseType(dto.getCourseType());
        }
        course.setHeadImageUrl(dto.getHeadImageUrl());
        return course;
    }

    public ActivityCourseVideo courseVideoSaveDTOToEntity(CourseVideoSaveDTO dto, ActivityCourseVideo video) {
        if (video == null) {
            video = new ActivityCourseVideo();
            video.setCreatedTime(new Date());
            video.setDeleted(false);
            video.setValid(0);
        }
        video.setName(dto.getName());
        video.setCoverUrl(dto.getCoverUrl());
        video.setResourceUrl(dto.getResourceUrl());
        video.setTotalDuration(dto.getTotalDuration());
        video.setDeleted(false);
        video.setPriority(dto.getPriority());
        video.setType(dto.getType());
        video.setCourseId(dto.getCourseId());
        video.setDescription(dto.getDescription());
        video.setName(dto.getName());
        video.setTryPlay(dto.getTryPlay());
        return video;
    }

    public List<UserCourseOrderDetailVo> orderToDetailVoList(List<UserCourseOrder> list, Map<String, String> map, Map<String, WechatMiniUserPO> userMap) {
        return list.stream().map(item -> {
            UserCourseOrderDetailVo detailVo = mapperFacade.map(item, UserCourseOrderDetailVo.class);
            String activityName = map.get(item.getActivityId());
            if (activityName != null) {
                detailVo.setActivityName(activityName);
            }
            WechatMiniUserPO userPO = userMap.get(item.getUserId());
            if (userPO != null) {
                detailVo.setUnionId(userPO.getUnionId());
                detailVo.setNickName(userPO.getNickName());
                if (StringUtils.isBlank(detailVo.getPhone())) {
                    detailVo.setPhone(userPO.getMobile());
                }
            }
            return detailVo;
        }).collect(Collectors.toList());
    }
}
